home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PsL Monthly 1993 December
/
PSL Monthly Shareware CD-ROM (December 1993).iso
/
prgmming
/
dos
/
c
/
filarry.exe
/
FILARRAY.CPP
< prev
next >
Wrap
C/C++ Source or Header
|
1993-04-16
|
3KB
|
123 lines
// filarray.cpp -- class FileArray implementation
// class FileArray serves as "File Array"
// Author : Jian Hua, 04/01/93
// Compiler : Borland C++ 3.1
// (c) Jian Hua. Source may be used freely if author is acknowledged.
// Object files may be used freely
#include <string.h>
#ifndef __MSGSTR_H
#include "filarray.h"
#endif
//
// constructor open file
// if open fail or the file is not expected file, set OK = -1
// so later method call no harmness
// this is kind of fault-tolerace
//
FileArray::FileArray(const char* fname)
{
is.open(fname, ios::in|ios::binary);
if( is.bad() ) {
#ifdef TEST
cout << "open file error";
#endif
OK = -1;
return;
}
memset(&headInfo, 0x0, sizeof(SDTHEADER));
is.read((char *)&headInfo, sizeof(SDTHEADER));
if (headInfo.id != 0xA1)
OK = -1;
else {
for(int i=0; i<5; i++)
ptrStr[i] = 0;
count = 0;
OK = 0;
}
}
//
// destructor close file
//
FileArray::~FileArray()
{
is.close();
for(int i=0; i<5; i++) {
if(ptrStr[i] != 0)
delete [] ptrStr[i];
}
}
//
// operator overloading at here serves as a "file array"
// to extract one string, index by ndx
// the largest string upto 512 character
// if any error occurs, return a null, so caller should check it
//
//
char* FileArray::operator[](int ndx)
{
if( OK != 0 )
return NULL;
if( ndx < 0 || ndx > headInfo.offsetArraySize )
return NULL;
char retStr[512];
memset(retStr, 0x0, sizeof(retStr));
long buf;
long spos = 0;
if( ndx == 0 ) {
spos = sizeof(SDTHEADER);
is.seekg(spos, ios::beg);
is.getline(retStr, sizeof(retStr), NULL);
updatePtr(strlen(retStr));
strcpy(ptrStr[count], retStr);
count += 1;
return ptrStr[count-1];
}
else
spos = sizeof(SDTHEADER) + headInfo.strCtxLen + ndx * 4;
is.clear();
is.seekg(spos, ios::beg);
buf = 0;
is.read((char *)&buf, sizeof(buf));
spos = sizeof(SDTHEADER) + buf;
is.seekg( (long)spos, ios::beg );
is.getline(retStr, sizeof(retStr), NULL);
updatePtr(strlen(retStr));
strcpy(ptrStr[count], retStr);
count += 1;
return ptrStr[count-1];
}
//
// here this I use a set of recycle buffers
// why I use this?
// take look at the example, such as, a function call may
// lokk like:
// func( strFunc(1), strFunc(2), strFunc(3) );
// here is the prototype of strFunc():
// char* strFunc(int num);
// so consider what will happen when you call func()
//
void FileArray::updatePtr(int len)
{
if( count == 4 )
count = 0;
if( ptrStr[count] != NULL )
delete[] ptrStr[count];
ptrStr[count] = new char[len+1];
}